#!/bin/bash
# BLAST序列搜索API测试脚本

echo "========================================="
echo "BLAST序列搜索API测试"
echo "========================================="
echo ""

API_URL="http://localhost:8000/api/search/sequence/"

# 测试1: 正常查询(使用已知序列的前31个氨基酸)
echo "测试1: 正常序列搜索"
echo "-------------------"
curl -X POST "$API_URL" \
  -F "sequence=MTKLNTALIDRLIAAGGMVVKKQAPQPTRSW" \
  -F "evalue=0.001" \
  -F "max_target_seqs=5" \
  2>/dev/null | python3 -m json.tool | head -50

echo ""
echo ""

# 测试2: 输入验证 - 空序列
echo "测试2: 空序列(应返回400错误)"
echo "-------------------"
curl -X POST "$API_URL" \
  -F "sequence=" \
  -F "evalue=0.001" \
  2>/dev/null | python3 -m json.tool

echo ""
echo ""

# 测试3: 输入验证 - 无效字符
echo "测试3: 无效字符(应返回400错误)"
echo "-------------------"
curl -X POST "$API_URL" \
  -F "sequence=INVALID123XYZ" \
  -F "evalue=0.001" \
  2>/dev/null | python3 -m json.tool

echo ""
echo ""

# 测试4: FASTA格式输入
echo "测试4: FASTA格式输入"
echo "-------------------"
curl -X POST "$API_URL" \
  -F "sequence=>test_protein
MTKLNTALIDRLIAAGGMVVKKQAPQPTRSWHDDPDDRDREVRQYEPDEVEY" \
  -F "evalue=0.001" \
  -F "max_target_seqs=3" \
  2>/dev/null | python3 -m json.tool | head -40

echo ""
echo ""

# 测试5: 缓存测试(第二次查询应更快)
echo "测试5: 缓存测试"
echo "-------------------"
echo "第一次查询(无缓存):"
time curl -X POST "$API_URL" \
  -F "sequence=MTKLNTALIDRLIAAGGMVVKKQAPQPTRSW" \
  -F "evalue=0.001" \
  -F "max_target_seqs=5" \
  2>/dev/null | python3 -c "import sys, json; data=json.load(sys.stdin); print(f\"cached: {data.get('cached')}, execution_time: {data.get('execution_time'):.3f}s\")"

echo ""
echo "第二次查询(应命中缓存):"
time curl -X POST "$API_URL" \
  -F "sequence=MTKLNTALIDRLIAAGGMVVKKQAPQPTRSW" \
  -F "evalue=0.001" \
  -F "max_target_seqs=5" \
  2>/dev/null | python3 -c "import sys, json; data=json.load(sys.stdin); print(f\"cached: {data.get('cached')}, execution_time: {data.get('execution_time'):.3f}s\")"

echo ""
echo ""
echo "========================================="
echo "测试完成"
echo "========================================="

